package com.my.hbase;

import com.my.hbase.conf.Config;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class CreateTable {

    public static void main(String[] args) throws Exception {
        Connection connection = Config.getConnection();
        Admin admin = connection.getAdmin();
        //TableName tableName = TableName.valueOf("test");//定义表名
        // TableDescriptor对象通过TableDescriptorBuilder构建；
        // TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
        // ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("data")).build();// 构建列族对象
        // tableDescriptor.setColumnFamily(family);//设置列族
        // admin.createTable(tableDescriptor.build());//创建表

        //构建表
        TableName tableName = TableName.valueOf("test");
        TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
        //构建列族对象
        ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("data")).build();
        //添加列簇
        tableDescriptor.setColumnFamily(family);
        //创建表
        admin.createTable(tableDescriptor.build());

    }

    public static void disOrDelete() throws Exception {
        Connection connection = Config.getConnection();
        Admin admin = connection.getAdmin();
        TableName tableName = TableName.valueOf("test");
        //禁用表
        admin.disableTable(tableName);
        //删除表
        admin.deleteTable(tableName);
    }
}
